package com.wqh.zeromq.inform;

import com.baosight.hdjni.HDErrcode;
import com.baosight.hdsdk.HDDataProvider;
import com.baosight.hdsdk.HDTagManager;
import com.baosight.hdsdk.common.HDTagDataType;
import com.baosight.hdsdk.domain.data.HDBasicTag;
import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.domain.data.HDRecord;
import com.baosight.hdsdk.exception.HDSdkException;
import com.wqh.zeromq.util.HDManager;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.json.JSONObject;
import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SubscriberRemote {
    public static void main(String[] args) {

        //获取所有服务器上的普通tag点数据
        HDDataConnection connection = HDManager.getConnection();
        HDTagManager manager = connection.getTagManager();
        HDDataProvider provider = new HDDataProvider(connection);
        HDErrcode errcode =new HDErrcode();
        List<ImmutablePair<HDRecord, Integer>> list=null;

        List<HDBasicTag>  initList = new ArrayList<>();
        List<Integer>  tagIdList = new ArrayList<>();
        Map<Integer, String> id_NameMap =new HashMap<>();
        Map<String, Object> map =new HashMap<>();
        try {
            initList = manager.queryBasicTagsSimple("*","*",HDTagDataType.INT8,true);
        } catch (HDSdkException e) {
            e.printStackTrace();
        }
        System.out.println("已获取所有普通tagid"+initList.size());
        for(HDBasicTag tag:initList){
            String tagName = tag.getName();
            int tagId = tag.getId();

            id_NameMap.put(tagId,tagName);
            //用于查询value
            tagIdList.add(tagId);
        }
        try {
            list = provider.querySnapshotsByIDs(tagIdList,errcode);
        } catch (HDSdkException e) {
            e.printStackTrace();
        }
        System.out.println("以获取所有普通tagid对应value");
        for(ImmutablePair pair:list){
            HDRecord record= (HDRecord)pair.getKey();
            int tagId=record.getBelongTagID();
            String tagValue=record.getValueStr();
            String tagName=id_NameMap.get(tagId);
            map.put(tagName,tagValue);
        }
        System.out.println("已获得服务器的tag初始数据");
        try (ZContext context = new ZContext()){
            //创建“发布/订阅”模式中的“订阅”的socket
            ZMQ.Socket subscriber = context.createSocket(SocketType.SUB);

            // 设置一个Socket标示,当subscriber重新连接之后可以从上次中断的地方读取到终断之后错过的数据
            //subscriber.setConnectRid(String.valueOf(new Random().nextInt(100))); // 在Java中暂时还不支持
            subscriber.connect("tcp://10.25.167.49:5502");

            //设置订阅主题,只接受信息头为Weather的消息
            String topicName = "";

            subscriber.subscribe(topicName.getBytes());
            while(!Thread.currentThread().isInterrupted()&& map.size() <= 1000){
                String message = subscriber.recvStr(0);
                System.out.println(message);

                String messageName = message.substring(0,message.indexOf("[")-1);
                String messageValue = message.substring(message.indexOf("[")+1,message.length()-1);

                JSONObject jsonObject =new JSONObject(messageValue);
                //System.out.println(messageName+":"+jsonObject.get("alias_11400"));
                Map<String , Object> newMap = jsonObject.toMap();

                for(String key : newMap.keySet()){
                    map.put(key,newMap.get(key));
                }
                System.out.println(message);
            }

        }
    }
}
